package org.example.kaobei.shallow;

/**
 * 浅拷贝示例
 */
public class ShallowCopy {
    public static void main(String[] args) throws CloneNotSupportedException {
        TeacherShallow teacher = new TeacherShallow();
        teacher.setName("riemann");
        teacher.setAge(28);

        StudentShallow student1 = new StudentShallow();
        student1.setName("edgar");
        student1.setAge(18);
        student1.setTeacher(teacher);

        StudentShallow student2 = (StudentShallow) student1.clone();
        System.out.println("-------------拷贝后-------------");
        System.out.println(student2.getName());
        System.out.println(student2.getAge());
        System.out.println(student2.getTeacher().getName());
        System.out.println(student2.getTeacher().getAge());

        System.out.println("-------------修改老师的信息后-------------");
        // 修改老师的信息
        teacher.setName("jack");

        // student1  student2 中的 teacher 都被修改为 jack 了
        // 结果分析： 两个引用student1和student2指向不同的两个对象，但是两个引用student1和student2中的两个teacher引用指向的是同一个对象，所以说明是浅拷贝。
        System.out.println("student1的teacher为： " + student1.getTeacher().getName());
        System.out.println("student2的teacher为： " + student2.getTeacher().getName());

    }
}